
#include "test3.h"

#include "midea_collection_view.h"
#include "midea_collection_dynamic.h"
#include "../base_scroll_ui/midea_flex.h"
#include "../base_scroll_ui/midea_base_scroll_tool.h"
#include "../base_scroll_ui/midea_base_scroll_cell.h"



static lv_obj_t* bg_view = NULL;
static lv_obj_t* collection_content = NULL;

void test3_update_collection(void *data, lv_obj_t *item_view, int32_t index, uint8_t ui_type)
{
    debug_d("update\n");
    // debug_d("index=%d\n",index);
    // debug_d("ui_type=%d\n",ui_type);
    // debug_d("data   =%s\n",data);
    // debug_d("item_view   =%x\n",item_view);

    switch(ui_type){
    case 1:
    {

        break;
    }
    case 2:
    {

        break;
    }
    case 3:
    {

        break;
    }
    default:break;
    }
}


void test3_install_collection(void *data, lv_obj_t *item_view, int32_t index, uint8_t ui_type)
{
    debug_d("install-index=%d\n",index);

    lv_coord_t x = 0;
    lv_coord_t y = 0;
    lv_coord_t pw = 0;
    lv_coord_t ph = 0;

    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);

    x = 0;//index*pw;
    y = 0;

    switch(ui_type)
    {
        case 1:
        {     
            lv_obj_t* test1 = lv_obj_create(item_view);
            lv_obj_set_pos(test1, x, y);
            lv_obj_set_size(test1,pw,ph);
            lv_obj_set_style_bg_opa(test1, LV_OPA_100, LV_PART_MAIN);
            lv_obj_set_style_bg_color(test1, lv_color_make(0xff,0,0), LV_PART_MAIN);

            lv_obj_set_style_radius(test1, 0, LV_PART_MAIN);
            lv_obj_set_style_border_width(test1, 0, LV_PART_MAIN);
            lv_obj_set_style_border_opa(test1, LV_OPA_0, LV_PART_MAIN);
            lv_obj_set_style_border_color(test1, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
            lv_obj_set_style_pad_all(test1,0,0);
            break;
        }
        case 2:
        {
            lv_obj_t* test2 = lv_obj_create(item_view);

            lv_obj_set_pos(test2, x, y);
            lv_obj_set_size(test2, pw, ph);
            lv_obj_set_style_bg_opa(test2, LV_OPA_100, LV_PART_MAIN);
            lv_obj_set_style_bg_color(test2, lv_color_make(0,0xff,0), LV_PART_MAIN);

            lv_obj_set_style_radius(test2, 0, LV_PART_MAIN);
            lv_obj_set_style_border_width(test2, 0, LV_PART_MAIN);
            lv_obj_set_style_border_opa(test2, LV_OPA_0, LV_PART_MAIN);
            lv_obj_set_style_border_color(test2, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
            lv_obj_set_style_pad_all(test2,0,0);
            break;
        }
        case 3:
        {
            lv_obj_t* test3 = lv_obj_create(item_view);
            lv_obj_set_pos(test3, x, y);
            lv_obj_set_size(test3, pw, ph);
            lv_obj_set_style_bg_opa(test3, LV_OPA_100, LV_PART_MAIN);
            lv_obj_set_style_bg_color(test3, lv_color_make(0,0,0xff), LV_PART_MAIN);
           
            lv_obj_set_style_radius(test3, 0, LV_PART_MAIN);
            lv_obj_set_style_border_width(test3, 0, LV_PART_MAIN);
            lv_obj_set_style_border_opa(test3, LV_OPA_0, LV_PART_MAIN);
            lv_obj_set_style_border_color(test3, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
            lv_obj_set_style_pad_all(test3,0,0);
            break;
        }
        case 4:
        {
            lv_obj_t* test4 = lv_obj_create(item_view);
            lv_obj_set_pos(test4, x, y);
            lv_obj_set_size(test4,pw,ph);
            lv_obj_set_style_bg_opa(test4, LV_OPA_100, LV_PART_MAIN);
            lv_obj_set_style_bg_color(test4, lv_color_make(0xff,0xff,0), LV_PART_MAIN);
            
            lv_obj_set_style_radius(test4, 0, LV_PART_MAIN);
            lv_obj_set_style_border_width(test4, 0, LV_PART_MAIN);
            lv_obj_set_style_border_opa(test4, LV_OPA_0, LV_PART_MAIN);
            lv_obj_set_style_border_color(test4, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
            lv_obj_set_style_pad_all(test4,0,0);
            break;
        }
        case 5:
        {
            lv_obj_t* test5 = lv_obj_create(item_view);
            lv_obj_set_pos(test5, x, y);
            lv_obj_set_size(test5, pw, ph);
            lv_obj_set_style_bg_opa(test5, LV_OPA_100, LV_PART_MAIN);
            lv_obj_set_style_bg_color(test5, lv_color_make(0,0xff,0xff), LV_PART_MAIN);
            
            lv_obj_set_style_radius(test5, 0, LV_PART_MAIN);
            lv_obj_set_style_border_width(test5, 0, LV_PART_MAIN);
            lv_obj_set_style_border_opa(test5, LV_OPA_0, LV_PART_MAIN);
            lv_obj_set_style_border_color(test5, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
            lv_obj_set_style_pad_all(test5,0,0);
            break;
        }
        case 6:
        {
            lv_obj_t* test6 = lv_obj_create(item_view);
            lv_obj_set_pos(test6, x, y);
            lv_obj_set_size(test6, pw, ph);
            lv_obj_set_style_bg_opa(test6, LV_OPA_100, LV_PART_MAIN);
            lv_obj_set_style_bg_color(test6, lv_color_make(0xff,0,0xff), LV_PART_MAIN);

            lv_obj_set_style_radius(test6, 0, LV_PART_MAIN);
            lv_obj_set_style_border_width(test6, 0, LV_PART_MAIN);
            lv_obj_set_style_border_opa(test6, LV_OPA_0, LV_PART_MAIN);
            lv_obj_set_style_border_color(test6, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
            lv_obj_set_style_pad_all(test6,0,0);
            break;
        }
        case 7:
        {
            lv_obj_t* test7 = lv_obj_create(item_view);
            lv_obj_set_pos(test7, x, y);
            lv_obj_set_size(test7, pw, ph);
            lv_obj_set_style_bg_opa(test7, LV_OPA_100, LV_PART_MAIN);
            lv_obj_set_style_bg_color(test7, lv_color_make(0xff,128,0xff), LV_PART_MAIN);

            lv_obj_set_style_radius(test7, 0, LV_PART_MAIN);
            lv_obj_set_style_border_width(test7, 0, LV_PART_MAIN);
            lv_obj_set_style_border_opa(test7, LV_OPA_0, LV_PART_MAIN);
            lv_obj_set_style_border_color(test7, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
            lv_obj_set_style_pad_all(test7,0,0);
            break;
        }
        default:break;
        }

}

void test3_free_data(void *data)
{
    debug_d("free\n");



}

static void collection_view_scroll_cb(lv_event_t *e)
{
    lv_obj_t *obj = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);

    // if (code == LV_EVENT_SCROLL_END)
    // {
    //     debug_d("scroll end\n");
    //     lv_obj_readjust_scroll(obj, LV_ANIM_ON);
    // }

    // lv_coord_t x = lv_obj_get_scroll_x(obj);
    // lv_coord_t y = lv_obj_get_scroll_y(obj);
    // printf("scrol:x:y=%d:%d\n",x,y);

}

int test3_main(void)
{
    lv_coord_t pw = 0;
    lv_coord_t ph = 0;

    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);

    bg_view = lv_obj_create(lv_scr_act());

    lv_obj_set_width(bg_view, pw);
    lv_obj_set_height(bg_view, ph);
    lv_obj_set_style_border_width(bg_view, 0, 0);
    lv_obj_set_style_radius(bg_view, 0, 0);
    lv_obj_center(bg_view);
    lv_obj_set_style_bg_color(bg_view, lv_color_hex(0xe8380d), 0);
    lv_obj_set_style_bg_opa(bg_view, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_all(bg_view, 0, 0);
    
    lv_obj_clear_flag(bg_view,LV_OBJ_FLAG_SCROLLABLE);

    #if 1
	lv_obj_set_style_bg_opa(bg_view, LV_OPA_0, LV_PART_MAIN);
    #else
	lv_obj_set_style_bg_opa(bg_view, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(bg_view, lv_color_make(0xff,0xff,0xff), LV_PART_MAIN);
    #endif

    
    lv_coord_t pad = 0;
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_pad_bottom(&style, pad);
    lv_style_set_pad_top(&style, pad);
    lv_style_set_pad_left(&style, 0);
    lv_style_set_pad_right(&style, 0);
    lv_style_set_border_width(&style, 0);
    lv_style_set_bg_color(&style,lv_color_hex(0xff0000));
    lv_style_set_bg_opa(&style,LV_OPA_100);

    collection_content = create_midea_collection("test3",bg_view,LV_DIR_HOR,
        test3_update_collection,test3_install_collection,test3_free_data);
    if(collection_content == NULL){
        debug_d("err0\n");
        return -1;
    }

    midea_collection_view *collection = (midea_collection_view*)collection_content;
    collection->dynamic_load_tag.load_tag = MIDEA_DYNAMIC_LOAD;

    lv_obj_add_style(collection_content, &style, LV_STATE_DEFAULT);

    // lv_obj_align(collection_content,LV_ALIGN_TOP_LEFT,0,0);//colletion组件第一个卡片贴住顶部
    // lv_obj_set_style_pad_row(collection_content, 0, LV_PART_MAIN);
    // lv_obj_set_style_pad_column(collection_content, 0,LV_PART_MAIN);
    // lv_obj_add_flag(collection_content, LV_OBJ_FLAG_SCROLL_ELASTIC | LV_OBJ_FLAG_SCROLL_CHAIN_HOR);
    // lv_obj_set_size(collection_content,480,320);

    #if 1
	lv_obj_set_style_bg_opa(collection_content, LV_OPA_0, LV_PART_MAIN);
    #else
	lv_obj_set_style_bg_opa(collection_content, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(collection_content, lv_color_make(0xff,0xff,0xff), LV_PART_MAIN);
    #endif

    add_midea_collection_data("test3", "data1", 1);
    add_midea_collection_data("test3", "data2", 2);
    add_midea_collection_data("test3", "data3", 3);
    add_midea_collection_data("test3", "data4", 4);
    add_midea_collection_data("test3", "data5", 5);
    add_midea_collection_data("test3", "data6", 6);
    add_midea_collection_data("test3", "data7", 7);

    // midea_collection_set_gap("test3",0,0);

    lv_obj_add_event_cb(collection_content, collection_view_scroll_cb, LV_EVENT_ALL, NULL);

    update_midea_collection("test3");
    scroll_collection_view_to_index("test3",1);

    // int num = 0;
    // num = midea_collection_get_data_count("test3");
    // debug_d("num=%d\n",num);

    // remove_midea_collection_data("test3",4);

    // num = midea_collection_get_data_count("test3");
    // debug_d("num=%d\n",num);

    return 0;

}


